package org.lq.ht.view.vip;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.ht.entity.Vip;
import org.lq.ht.service.VipService;
import org.lq.ht.service.impl.VipServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class V_vipLogin extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	private Vip vip;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_vipLogin frame = new V_vipLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_vipLogin() {
		setTitle("会员登陆");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 374, 240);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("编号：");
		lblNewLabel.setBounds(62, 53, 67, 15);
		contentPane.add(lblNewLabel);

		textField = new JTextField();
		textField.setBounds(120, 50, 131, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("密 码：");
		lblNewLabel_1.setBounds(62, 96, 54, 15);
		contentPane.add(lblNewLabel_1);

		passwordField = new JPasswordField();
		passwordField.setBounds(120, 93, 131, 21);
		contentPane.add(passwordField);

		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password = new String(passwordField.getPassword());
				//确定
				if(!textField.getText().equals("")) {
					if(password.equals(vip.getPassword())) {
						System.out.println("登陆成功！");
					}else {
						int v = JOptionPane.showConfirmDialog(V_vipLogin.this, "输入密码错误！是否重置！" );
						if(v==0) {
							down();
						}else {
							up();
						}
					}
				}else {
					JOptionPane.showMessageDialog(V_vipLogin.this, "请检索VIP");
				}
			}
		});
		btnNewButton.setBounds(82, 154, 93, 23);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取消
				textField.setText("");
				passwordField.setText("");
				up();
			}
		});
		btnNewButton_1.setBounds(221, 154, 93, 23);
		contentPane.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("检索");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//检索
				V_VipMain main =new V_VipMain();
				main.setBounds(V_vipLogin.this.getX(), V_vipLogin.this.getY(), main.getWidth(), main.getHeight());
				main.setVisible(true);
				main.addWindowListener(new WindowAdapter() {

					@Override
					public void windowClosed(WindowEvent e) {
						// TODO Auto-generated method stub
						int vid = main.getVid();
						VipService vs = new VipServiceImpl();
						vip = vs.getByVid(vid);
						textField.setText(vid+"");
					}

				});
			}
		});
		btnNewButton_2.setBounds(261, 49, 72, 23);
		contentPane.add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("重置密码");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//密码重置
				int index = JOptionPane.showConfirmDialog(V_vipLogin.this, "请确认是否重置！" );
				if(index==0) {
					//密码重置
					vip.setPassword("666666");
					VipService vs = new VipServiceImpl();
					if(vs.updateVip(vip)) {
						JOptionPane.showMessageDialog(V_vipLogin.this, "初始化成功！\n初始化密码为666666" );
						up();
					}else {
						JOptionPane.showMessageDialog(V_vipLogin.this, "初始化密码失败！" );
					}
				}else {
					up();
				}
			}
		});
		btnNewButton_3.setBackground(Color.RED);
		btnNewButton_3.setForeground(Color.ORANGE);
		btnNewButton_3.setBounds(147, 213, 93, 23);
		contentPane.add(btnNewButton_3);
	}

	//下降
	public void down() {
		int height = 250;
		while(height <= 300) {
			height+=5;
			this.setSize(this.getWidth(), height);
		}
	}
	//上升
	public void up() {
		int height = 300;
		while(height >=250) {
			height-=5;
			this.setSize(this.getWidth(), height);
		}
	}
}
